package player

import (
	"GameServer/Common/data"
	"GameServer/Common/utils"
	"GameServer/Game/errors"
	"GameServer/Game/lib"
	"GameServer/Game/m"
	"GameServer/Game/sp"

	log "github.com/sirupsen/logrus"
)

// 9013 客户端请求新手引导数据
func (p *Player) NewGuideRequest(req *sp.NewGuideRequest) (res *sp.NewGuideResponse) {
	uNewGuide := p.User.GetNewGuide()
	res = &sp.NewGuideResponse{
		Content:    uNewGuide.Content,
		SlotGuidId: uNewGuide.SlotGuidId,
		SlotEvent:  uNewGuide.SlotEvent,
		RewardCnt:  uNewGuide.RewardCnt,
	}
	if uNewGuide.SlotFlag {
		res.SlotFlag = 1
	}
	if uNewGuide.SlotRewardFlag {
		res.SlotRewardFlag = 1
	}

	return
}

// 9017 服务器主动推送新手引导数据
func (p *Player) NewGuideInfoPushToPlayer() {
	uNewGuide := p.User.GetNewGuide()
	res := &sp.NewGuideInfoPushRequest{
		Content: uNewGuide.Content,
	}
	if uNewGuide.SlotFlag {
		res.SlotFlag = 1
	}
	if uNewGuide.SlotRewardFlag {
		res.SlotRewardFlag = 1
	}
	uNewGuide.Update(uNewGuide)
	p.Send(res)
}

// 9014 客户端更新新手引导数据
func (p *Player) NewGuideUpdateRequest(req *sp.NewGuideUpdateRequest) (res *sp.NewGuideUpdateResponse) {
	uNewGuide := p.User.GetNewGuide()
	uNewGuide.Content = req.Content
	uNewGuide.Update(uNewGuide)
	res = &sp.NewGuideUpdateResponse{}
	return
}

// 9015 客户端请求，老虎机新手引导
func (p *Player) NewGuildSlotRequest(req *sp.NewGuildSlotRequest) (res *sp.NewGuildSlotResponse) {
	uNewGuide := p.User.GetNewGuide()
	switch req.Step {
	case 1:
		p.PutData("newGuildslotKey", uint64(222))
	case 2:
		p.PutData("newGuildslotKey", uint64(666))
		p.PutData("newGuildFireShield", int(0)) // 没有盾牌保护
	case 3:
		p.PutData("newGuildslotKey", uint64(333))
	case 4:
		p.PutData("newGuildslotKey", uint64(555))
	case 5:
		p.PutData("newGuildslotKey", uint64(110))
	case 99:
		uNewGuide.SlotFlag = true
		uNewGuide.Update(uNewGuide)
		p.PushAllActActivityInfo() // 所有活动信息
	}
	res = &sp.NewGuildSlotResponse{}

	if uNewGuide.SlotFlag {
		res.SlotFlag = 1
	}
	return
}

// 9016 新手引导结束
func (p *Player) NewGuidSlotResRequest(req *sp.NewGuidSlotResRequest) (res *sp.NewGuidSlotResResponse) {
	uNewGuide := p.User.GetNewGuide()

	if !uNewGuide.SlotFlag {
		//新手引导未完成
		res = &sp.NewGuidSlotResResponse{Errcode: data.Errcode.ServerErr}
		log.WithFields(log.Fields{
			"uid":    p.User.Uid.Hex(),
			"method": "NewGuidSlotResRequest",
			"data":   req,
		}).Error("新手引导未完成")
		return res
	}

	if uNewGuide.SlotRewardFlag {
		//新手引导奖励已经领取
		log.WithFields(log.Fields{
			"uid":    p.User.Uid.Hex(),
			"method": "NewGuidSlotResRequest",
			"data":   req,
		}).Error("新手引导奖励已经领取")
		res = &sp.NewGuidSlotResResponse{
			Errcode: data.Errcode.ServerErr,
		}
		return res
	}

	tpl, ok := data.Get("NewGuidRewards", uint64(1))
	if !ok {
		res = &sp.NewGuidSlotResResponse{Errcode: data.Errcode.ConfigNotFound}
		log.WithFields(log.Fields{
			"uid":    p.User.Uid.Hex(),
			"method": "NewGuidSlotResRequest",
			"data":   req,
		}).Error("NewGuidRewards配置表没有找到id:1")
		return res
	}
	t := tpl.(data.NewGuidReward)

	datas := make([]m.BaseKV, 0)
	for _, item := range t.Rewards {
		datas = append(datas, m.BaseKV{Key: utils.Int64(item.Key), Val: utils.Int64(item.Value)})
	}
	uProp, resGoods, boxRes, err := lib.GiveGoodsByKvList("NewGuidSlotResRequest", p.User, datas, p.Send)
	if err != nil {
		res = &sp.NewGuidSlotResResponse{
			Errcode: errors.ErrCode(err),
		}
		return res
	}
	// 有成就完成，进行通知
	achLst := lib.AchievComplete(p.User)
	if len(achLst) > 0 {
		p.Send(&sp.AchievementNoticeRequest{
			Lst: achLst,
		})
	}
	if uProp != nil {
		p.TimelyUpdateProp(req.GetType(), uProp)
		utils.AsyncFunc(func() {
			p.UpdateProp(req.GetType(), uProp)
		})
	}

	res = &sp.NewGuidSlotResResponse{
		Res:            lib.FormatGoodsInfoSps(resGoods),
		BoxRes:         lib.FormatBoxResSps(boxRes),
		SlotRewardFlag: 1,
	}

	uNewGuide.SlotRewardFlag = true
	uNewGuide.Update(uNewGuide)
	utils.AsyncFunc(func() {
		p.RefreshSlotUser()
	})
	return res
}
